
#ifndef _INTERACTION_LOGIC_
#define _INTERACTION_LOGIC_

#include <ttg/math.h>
#include "Constants.h"

using namespace ttg::math;

class InteractionLogic
{
	public:
		//Interacts this body with another (calculates new acceleration).
		//Updates acceleration of the first body.
		static TTG_INLINE void interactWith(float &p1_x, float &p1_y, float &p1_z,
											float &a1_x, float &a1_y, float &a1_z,
											const float &mass1,
											float &p2_x, float &p2_y, float &p2_z,
											const float &mass2)
		{
			vec3f radius = vec3f(p2_x - p1_x, p2_y - p1_y, p2_z - p1_z);
			float radius_length = radius.getLength() + EPSILON;
			vec3f tmp = radius * (GRAVITY_COEFF /
						(radius_length * radius_length * radius_length));
			a1_x += tmp.x * mass2;
			a1_y += tmp.y * mass2;
			a1_z += tmp.z * mass2;
		}
		//Interacts bodies (calculates new accelerations).
		//Updates accelerations only.
		static TTG_INLINE void interactWithEachOther(float &p1_x, float &p1_y, float &p1_z,
													 float &a1_x, float &a1_y, float &a1_z,
													 const float &mass1,
													 float &p2_x, float &p2_y, float &p2_z,
													 float &a2_x, float &a2_y, float &a2_z,
													 const float &mass2)
		{
			vec3f radius = vec3f(p2_x - p1_x, p2_y - p1_y, p2_z - p1_z);
			float radius_length = radius.getLength() + EPSILON;
			vec3f tmp = radius * (GRAVITY_COEFF /
						(radius_length * radius_length * radius_length));
			a1_x += tmp.x * mass2;
			a1_y += tmp.y * mass2;
			a1_z += tmp.z * mass2;
			a2_x -= tmp.x * mass1;
			a2_y -= tmp.y * mass1;
			a2_z -= tmp.z * mass1;
		}
		//Updates body's velocity and position, zeroes acceleration.
		static TTG_INLINE void update(float &p_x, float &p_y, float &p_z,
									  float &v_x, float &v_y, float &v_z,
									  float &a_x, float &a_y, float &a_z,
									  float dt)
		{
			//x
			v_x = (v_x + a_x * dt) * DAMPING;
			p_x += v_x * dt;
			a_x = 0.0f;
			//y
			v_y = (v_y + a_y * dt) * DAMPING;
			p_y += v_y * dt;
			a_y = 0.0f;
			//z
			v_z = (v_z + a_z * dt) * DAMPING;
			p_z += v_z * dt;
			a_z = 0.0f;
		}
};

#endif
